<?php

namespace App\Http\Resources\Common;

use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class WeatherResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $areaRepo = app(AreaRepositoryInterface::class);
        $weather  = parent::toArray($request);
        if (isset($weather['city'])) {
            $weather['city_name'] = $weather['city'];
            $city                 = $areaRepo->findByName($weather['city']) ?: $areaRepo->fuzzyFind($weather['city'], 'city');
            if ($city) {
                $weather['city'] = $city->getAreaNameUy();
            }
        }
        return $weather;
    }
}
